<script setup lang='ts'>
  import { onActivated, onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, $router } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import xmImage from '@/components/xmImage/index.vue'
  import Pagination from "@/components/Pagination/index.vue";
  import floorRewardsForm from './floorRewardsForm.vue';
  const floorRewardsFormRef = ref()
  const action = reactive({
    total:0,
    activityID:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    bridge_id:'' as any,
  })
  const isRoundType = [
    {type:0,label:'否'},
    {type:1,label:'是'},
  ],
  rewardType = [
    {type:1,label:'头像框'},
    {type:2,label:'座驾'},
    {type:3,label:'礼物'},
    {type:4,label:'气泡'},
    {type:5,label:'进场特效'},
    // {type:6,label:'金豆'},
    {type:7,label:'钻石'},
    {type:8,label:'装饰背景卡'},
  ],
  isSenderType = [
    {type:0,label:'送礼方'},
    {type:1,label:'收礼方'},
    {type:2,label:'收送双方'},
  ]
  const handleForm = async(row:any = {})=>{
    const { id=listQuery.bridge_id } = row
    floorRewardsFormRef.value.openDialog({
      ...row,
      id,
      isRoundType,
      rewardType,
      isSenderType
    })
  }
  const handleDelete = (row:any = {})=>{
    const { bridge_id=0, b_award_id=0,level='' } = row
    if(!b_award_id) return ToastWarning('缺少b_award_id')
    ElMessageBox.confirm(`确定删除盖楼配置ID：${bridge_id}-楼层：${level}的奖励配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.customBridgeAwardDel({b_award_id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const handleSerach = async()=>{
    action.list = []
    action.total = 0
    listQuery.page = 1
    await getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.customBridgeAwardList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data.map((res:any)=>{
      const { award_json='' } = res
      let awardList = []
      if(award_json) awardList = JSON.parse(award_json)
      return {
        ...res,
        awardList
      }
    })
  }
  let primarySet = false
  const primaryGetList = async()=>{
    if(primarySet) return
    primarySet = true
    const bridgeID = $router().currentRoute.value.query?.bridgeID || ''
    if(listQuery.bridge_id != bridgeID && bridgeID) {
      listQuery.bridge_id = bridgeID
      const loading = Toastloading()
      await getList()
      primarySet = false
      loading.close()
    }
  }
  onMounted(async()=>{
    primaryGetList()
  })
  onActivated(async()=>{
    primaryGetList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">盖楼配置ID：</div>
          <el-input type="text" v-model="listQuery.bridge_id" placeholder="请输入盖楼配置ID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" v-rule="'stencil_floorRewardsAdd'" @click="handleForm()">新增楼层奖励</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
    <el-table-column width="120" prop="bridge_id" label="盖楼配置ID" />
    <el-table-column width="120" prop="b_award_id" label="奖励ID" />
      <el-table-column width="180" prop="level" label="楼层" />
      <el-table-column width="120" prop="is_round" label="是否循环楼层">
        <template #default="scoped">
          {{ isRoundType.find(res=>res.type == scoped.row.is_round)?.label || scoped.row.is_round }}
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="award_json" label="楼层奖励">
        <template #default="scoped">
          <div class="xm-mb10" v-for="item in scoped.row.awardList">
            <el-tag>
              奖励方:{{ isSenderType.find(res=>res.type == item.is_sender)?.label || item.is_sender}}
              奖励类型:{{ rewardType.find(res=>res.type == item.send_type)?.label || item.send_type}}
              奖励ID：{{ item.goods_id }}
              奖励数量/时间：{{ item.goods_num }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="280" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="primary" v-rule="'stencil_floorRewardsEdit'" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" v-rule="'stencil_floorRewardsDel'" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <floorRewardsForm ref="floorRewardsFormRef" @update-list="getList" />
  </div>
  
</template>